package com.zmx.patients.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patients.BaseActivity;
import com.zmx.patients.R;
import com.zmx.patients.adapter.AddressListAdapter;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.AddressList;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class AddressListActivity extends BaseActivity {

    @BindView(R.id.re_view)
    RecyclerView reView;
    @BindView(R.id.empty_image)
    ImageView emptyImage;
    @BindView(R.id.registered)
    Button registered;

    private AddressListAdapter adapter;
    private List<AddressList> lists;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_list;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        adapter = new AddressListAdapter(mActivity);
        lists = new ArrayList<>();
        reView.setLayoutManager(new LinearLayoutManager(mActivity));
        reView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Intent intent = new Intent();
                intent.putExtra("id", lists.get(listPosition).getId()+"");
                intent.putExtra("name", lists.get(listPosition).getName());
                intent.putExtra("phone", lists.get(listPosition).getPhone());
                intent.putExtra("address", lists.get(listPosition).getProvince()+lists.get(listPosition).getCounty()+lists.get(listPosition).getCity()+lists.get(listPosition).getStreet());
                //这里是在Recycleview的适配器里，传了一个Activity才能用方法setResult
                mActivity.setResult(Activity.RESULT_OK,intent);
                mActivity.finish(); //关闭活动

            }
        });


    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        lists.clear();

                        JSONObject object = new JSONObject(msg.obj.toString());

                        JSONArray data = object.getJSONArray("data");
                        Gson gson = new Gson();
                        for (int i = 0; i < data.length(); i++) {

                            AddressList al = gson.fromJson(data.getJSONObject(i).toString(), AddressList.class);

                            JSONObject j_province = new JSONObject(data.getJSONObject(i).getString("province"));
                            al.setProvince(j_province.getString("areaName"));
                            JSONObject j_city = new JSONObject(data.getJSONObject(i).getString("city"));
                            al.setCity(j_city.getString("areaName"));
                            JSONObject j_county = new JSONObject(data.getJSONObject(i).getString("county"));
                            al.setCounty(j_county.getString("areaName"));
                            lists.add(al);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getAddress() {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getAddress");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 1, 404);


    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddress();
    }

    @OnClick(R.id.registered)
    public void onViewClicked() {

        startActivity(AddAddressActivity.class);

    }
}
